﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.BOM;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.BOMCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.BOMCommandHandler
{
    /// <summary>
    /// 工艺流程
    /// 物联网-智能MES生产管理系统-01-BOM管理V1.1-20250206
    /// </summary>
    public class ProcessInfoCommandHandler : IRequestHandler<ProcessInfoCommand, APIResult<int>>
    {
        private readonly IBaseRepository<ProcessInfo> processRep;
        private readonly IMapper mapper;
        public ProcessInfoCommandHandler(IBaseRepository<ProcessInfo> processRep, IMapper mapper)
        {
            this.processRep = processRep;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(ProcessInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            var res=mapper.Map<ProcessInfo>(request);
            var model = processRep.Add(res).Result;
            if (model>0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data=model;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = model;
            }
            return Task.FromResult(result);
        }
    }
}
